﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Xml;
using NColony.Client.Wpf.ViewModels.Contracts;
using NColony.Syndication.Receiver.Contracts;

namespace NColony.Client.Wpf.ViewModels
{
    /// <summary>
    /// View model for the Main Window.
    /// </summary>
    [Export(typeof(IMainWindowViewModel))]
    public sealed class MainWindowViewModel : IMainWindowViewModel
    {
        private readonly ISyndicationReader<ISyndicationFeed> reader;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
        /// </summary>
        /// <param name="reader">The reader.</param>
        [ImportingConstructor]
        public MainWindowViewModel(ISyndicationReader<ISyndicationFeed> reader)
        {
            if (reader == null) throw new ArgumentNullException("reader");

            this.reader = reader;
        }

        /// <summary>
        /// Gets the feed reader.
        /// </summary>
        /// <value>The feed reader.</value>
        public ISyndicationReader<ISyndicationFeed> FeedReader
        {
            get
            {
                return this.reader;
            }
        }

        /// <summary>
        /// Loads the feed reader.
        /// </summary>
        /// <param name="feedAddress">The feed address.</param>
        public void LoadFeedReader(string feedAddress)
        {
            var settings = new XmlReaderSettings
            {
                DtdProcessing = DtdProcessing.Ignore
            };

            using (var xmlReader = XmlReader.Create(feedAddress, settings))
            {
                this.OnPropertyChanging("FeedReader");
                this.FeedReader.Read(xmlReader);
                this.OnPropertyChanged("FeedReader");
            }
        }

        /// <summary>
        /// Occurs when a property value is changing.
        /// </summary>
        public event PropertyChangingEventHandler PropertyChanging;

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Called when [property changing].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        private void OnPropertyChanging(string propertyName)
        {
            if (this.PropertyChanging != null)
            {
                this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
